from matplotlib.pyplot import title
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import plotly.offline as of  
import numpy as np
import plotly.express as px
#=====================================================
def rmse(data):
    mean=np.mean(data)
    for i in data:
        rmse_=(mean-i)**2
    rmse_=np.sqrt(rmse_/len(data))
    return rmse_
def visual(data1,data2,text):
    
    fig =make_subplots(1,2)
    fig.add_trace(go.Bar(
        name='<br>CPU<br>Intel(R) Core(TM)<br>i5-9300H CPU<br>@2.40GHz<br>',
        x=["TensorFlow","NumPy","PyTorch","PyFFTW<br>3 threads",], y=[np.mean(data1[0]),np.mean(data1[1]),np.mean(data1[2]),np.mean(data1[3])],
        error_y=dict(type='data', array=[rmse(data1[0]),rmse(data1[1]),rmse(data1[2]),rmse(data1[3])],),
        # marker_color='#4f63ec',
         textfont_size=13,
        texttemplate=[
        str(np.float64(format(np.mean(data1[0]), '.3g')))+"±"+str(float(format(rmse(data1[0]), '.3g'))),
        str(float(format(np.mean(data1[1]), '.3g')))+"±"+str(float(format(rmse(data1[1]), '.3g'))),
        str(float(format(np.mean(data1[2]), '.3g')))+"±"+str(float(format(rmse(data1[2]), '.3g'))),
        str(float(format(np.mean(data1[3]), '.3g')))+"±"+str(float(format(rmse(data1[3]), '.3g')))
        ]
    ),1,1)
    fig.add_trace(go.Bar(
        name='<br>GPU<br>NVIDIA GeForce<br>GTX 1650<br>',
        x=["TensorFlow","CuPy","PyTorch"], y=[np.mean(data2[0]),np.mean(data2[1]),np.mean(data2[2])],
        error_y=dict(type='data', array=[rmse(data2[0]),rmse(data2[1]), rmse(data2[2]) ],),
        #marker_color='#1327b3',
        textfont_size=13,
        texttemplate=[
        str(float(format(np.mean(data2[0]), '.3g')))+"±"+str(float(format(rmse(data2[0]), '.3g'))),
        str(float(format(np.mean(data2[1]), '.3g')))+"±"+str(float(format(rmse(data2[1]), '.3g'))),
        str(float(format(np.mean(data2[2]), '.3g')))+"±"+str(float(format(rmse(data2[2]), '.3g')))
        ]
    ),1,2)
    fig.add_annotation(text=text,
                  xref="paper", yref="paper",
                  x=1, y=1, 
                  font=dict(
                    size=20,
                    ),
                  showarrow=False)
    fig.update_xaxes(tickfont_size=20)
    fig.update_yaxes(tickfont_size=20,title="average time")
    fig.update_layout(barmode='group' ,   font=dict(
        size=20,
    ),)
    fig.show()
#=====================================================
if __name__=="__main__":
    tf_cpu=np.load("tf_cpu(79, 79, 79).npy")
    tf_gpu=np.load("tf_gpu(79, 79, 79).npy")
    torch_cpu=np.load("torch_cpu(79, 79, 79).npy")
    torch_gpu=np.load("torch_gpu(79, 79, 79).npy")
    np_cpu=np.load("np_cpu(79, 79, 79).npy")
    cp_gpu=np.load("cp_gpu(79, 79, 79).npy")
    fftw3=np.load("fftw3(79, 79, 79).npy")
    visual([tf_cpu,np_cpu,torch_cpu,fftw3,],[tf_gpu,cp_gpu,torch_gpu],"tensor shape:(79, 79, 79)<br>number:10<br>repeat:100<br>error:rmse")
